package com.ruoyi.community.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.community.domain.OwnerNotice;
import com.ruoyi.community.domain.OwnerVote;
import com.ruoyi.community.mapper.OwnerVoteMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.community.mapper.OwnerMemberVoteMapper;
import com.ruoyi.community.domain.OwnerMemberVote;
import com.ruoyi.community.service.IOwnerMemberVoteService;

/**
 * 投票详情Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-08-24
 */
@Service
public class OwnerMemberVoteServiceImpl implements IOwnerMemberVoteService 
{
    @Autowired
    private OwnerMemberVoteMapper ownerMemberVoteMapper;

    @Autowired
    private OwnerVoteMapper ownerVoteMapper;

    /**
     * 查询投票详情
     * 
     * @param id 投票详情ID
     * @return 投票详情
     */
    @Override
    public OwnerMemberVote selectOwnerMemberVoteById(String id)
    {
        return ownerMemberVoteMapper.selectOwnerMemberVoteById(id);
    }


    /**
     * 查询投票详情列表
     * 
     * @param ownerMemberVote 投票详情
     * @return 投票详情
     */
    @Override
    public List<OwnerMemberVote> selectOwnerMemberVoteList(OwnerMemberVote ownerMemberVote)
    {
        return ownerMemberVoteMapper.selectOwnerMemberVoteList(ownerMemberVote);
    }

    /**
     * 新增投票详情
     * 
     * @param ownerMemberVote 投票详情
     * @return 结果
     */
    @Override
    public int insertOwnerMemberVote(OwnerMemberVote ownerMemberVote)
    {
        int hasVoted = ownerMemberVoteMapper.hasVoted(ownerMemberVote.getUserId().toString(), ownerMemberVote.getVoteId());
        if(hasVoted==1){
            return -1;
        }else{
            int result = ownerMemberVoteMapper.insertOwnerMemberVote(ownerMemberVote);
            OwnerVote tmp = ownerVoteMapper.selectOwnerVoteById(ownerMemberVote.getVoteId());
            tmp.setVoteNumber(tmp.getVoteNumber()+1);
            ownerVoteMapper.updateOwnerVote(tmp);
            return result;
        }

    }

    /**
     * 修改投票详情
     * 
     * @param ownerMemberVote 投票详情
     * @return 结果
     */
    @Override
    public int updateOwnerMemberVote(OwnerMemberVote ownerMemberVote)
    {
        return ownerMemberVoteMapper.updateOwnerMemberVote(ownerMemberVote);
    }

    /**
     * 批量删除投票详情
     * 
     * @param ids 需要删除的投票详情ID
     * @return 结果
     */
    @Override
    public int deleteOwnerMemberVoteByIds(String[] ids)
    {
        return ownerMemberVoteMapper.deleteOwnerMemberVoteByIds(ids);
    }

    /**
     * 删除投票详情信息
     * 
     * @param id 投票详情ID
     * @return 结果
     */
    @Override
    public int deleteOwnerMemberVoteById(String id)
    {
        return ownerMemberVoteMapper.deleteOwnerMemberVoteById(id);
    }

    @Override
    public Integer getVoteMemberTotal(String id)
    {
        return ownerMemberVoteMapper.getVoteMemberTotal(id);
    }

    @Override
    public Integer getFinishedMember(String id) {
        return ownerMemberVoteMapper.getFinishedMember(id);
    }

    @Override
    public String getOptionStats(String id) {
        List<OwnerMemberVote> l = ownerMemberVoteMapper.getVoteRecordList(id);
        String options = ownerVoteMapper.selectOwnerVoteById(id).getOptions();
        int optionNum = options.split(",").length;
        int optionStats[] = new int[optionNum];
        for(OwnerMemberVote record:l){
            optionStats[Integer.parseInt(record.getOption())] ++;
        }
        StringBuilder result = new StringBuilder(String.valueOf(optionStats[0]));
        for(int i=1; i<optionNum; i++){
            result.append(",");
            result.append(optionStats[i]);
        }
        return result.toString();
    }
}
